package com.cheng.example.atguigu.design_pattern.principle.inversion.improve;

/*
 * @author 程帅
 * @createTime: 2020/10/18 14:32
 * @Created: with IntelliJ IDEA.
 */
public class DependencyPass {
    public static void main(String[] args) {
        //方式一
//        ITV itv = new ChangHong();
//        IOpenAndClose iOpenAndClose = new OpenAndClose();
//        iOpenAndClose.open(itv);

        //方式二
//        ITV itv = new ChangHong();
//        IOpenAndClose iOpenAndClose = new OpenAndClose(itv);
//        iOpenAndClose.open();

        //方式三
        ITV itv = new ChangHong();
        IOpenAndClose iOpenAndClose = new OpenAndClose();
        iOpenAndClose.setTv(itv);
        iOpenAndClose.open();

    }
}

////方式1：通过接口传递实现依赖
//interface ITV {//ITV接口
//     void play();//抽象方法
//}
////开关的接口
//interface IOpenAndClose {
//    void open(ITV tv);//抽象方法，接收接口
//}
////实现接口
//class ChangHong implements ITV{
//    @Override
//    public void play() {
//        System.out.println("长虹电视机，打开");
//    }
//}
//class OpenAndClose implements IOpenAndClose {
//    @Override
//    public void open(ITV tv) {
//        tv.play();
//    }
//}


//// 方式2: 通过构造方法依赖传递
//interface IOpenAndClose{
//    void open();//抽象方法
//}
//
//interface ITV{ //ITV接口
//    void play();
//}
//
//class OpenAndClose implements IOpenAndClose{
//
//    public ITV tv;//成员
//
//    public OpenAndClose(ITV tv) { //构造器
//        this.tv = tv;
//    }
//
//    @Override
//    public void open() {
//        this.tv.play();
//    }
//}
//
//class ChangHong implements ITV{
//
//    @Override
//    public void play() {
//        System.out.println("长虹电视机，打开");
//    }
//}


// 方式3: 通过set方法依赖传递
interface IOpenAndClose{
    void open();//抽象方法
    void setTv(ITV tv);
}

interface ITV{ //ITV接口
    void play();
}

class ChangHong implements ITV{

    @Override
    public void play() {
        System.out.println("长虹电视机，打开");
    }
}

class OpenAndClose implements IOpenAndClose{

    private ITV tv;

    @Override
    public void open() {
        this.tv.play();
    }

    @Override
    public void setTv(ITV tv) {
        this.tv = tv;
    }
}

